<?php

class BitacoraController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','view','index', 'pdf', 'excel', 'backup'),
				'expression' => '(Yii::app()->user->perfil==1)',					
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}


	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Bitacora');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Bitacora('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Bitacora']))
			$model->attributes=$_GET['Bitacora'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Bitacora the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Bitacora::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Bitacora $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='bitacora-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

	//Bitacora en pdf
	public function actionPdf()
    {
    	$dataProvider = $_SESSION['datos']->getData();
        $this->render('pdf',array('dataProvider'=>$dataProvider),true);
    }

    //Bitacora en excel
	public function actionExcel()
    {
		$dataProvider = $_SESSION['datos']->getData();

		Yii::app()->request->sendFile('Bitacora.xls',
        $this->renderPartial('excelReport',array('dataProvider'=>$dataProvider), true)
        );       		

    }

    //Respaldo de bd
    public function actionBackup()
	{
		try{
		Yii::import('ext.dumpDB.dumpDB');
  		$dumper = new dumpDB();
  		$dumper->getDump(true);
  		echo $dumper->getDump(false);
  		date_default_timezone_set("America/Costa_Rica"); 
		$fecha=date('Y-m-d H:i:s');
  		Yii::app()->db->createCommand("Insert into bitacora (IDUSUARIO, Accion, Descripcion, Fecha) Values('".Yii::app()->user->id."','5','Backup de la información','$fecha')")->queryScalar();
		Yii::app()->user->setFlash('success','Se creo correctamente el backup');

  		}
  		catch(exception $e){
  					Yii::app()->user->setFlash('error','Se presentaron problemas al realizar el backup');
  		}
	}
}
